// var events = require("events");
// var Emitter = events.EventEmitter;
// function Dog(dogName){
//     Emitter.call(this);//实现对EventEmitter继承
//     this.dogName = dogName;
// }
// Dog.prototype = Emitter.prototype;//实现对EventEmitter的继承
// Dog.prototype.bark = function(){
//     console.log(this.dogName = "barked");
// }
// module.exports = Dog;//对外公布Dog沟通函数
var events = require("events");
var EventEmitter = events.EventEmitter;

function Dog(name,energy) {
    EventEmitter.call(this);
    this.name = name;
    this.energy = energy;
    var interValId = setInterval(() =>{
        /*
        *function函数，函数内this指向取决于通用主体
        *箭头函数，函数内this指向函数定义的上下文
        */
        console.log(this);
        if(this.energy >= 0) {
            this.emit("bark");
            this.energy--;
        }else{
            clearInterval(interValId);
        }
    },1000)
}

for(var i in EventEmitter.prototype) {
    Dog.prototype[i] = EventEmitter.prototype[i];
}
module.exports = Dog;